import { Space, Button } from 'antd';
import Gconfirm from '@/component/confirm'
import styles from './index.less';

// 初始化表单数据
export function initListenerForm(listener) {
  let self = {
    ...listener
  };
  if (listener.script) {
    self = {
      ...listener,
      ...listener.script,
      scriptType: listener.script.resource ? "externalScript" : "inlineScript"
    };
  }
  if (listener.event === "timeout" && listener.eventDefinitions) {
    if (listener.eventDefinitions.length) {
      let k = "";
      for (let key in listener.eventDefinitions[0]) {
        console.log(listener.eventDefinitions, key);
        if (key.indexOf("time") !== -1) {
          k = key;
          self.eventDefinitionType = key.replace("time", "").toLowerCase();
        }
      }
      console.log(k);
      self.eventTimeDefinitions = listener.eventDefinitions[0][k].body;
    }
  }
  return self;
}

export function initListenerType(listener) {
  let listenerType;
  if (listener.class) listenerType = "classListener";
  if (listener.expression) listenerType = "expressionListener";
  if (listener.delegateExpression) listenerType = "delegateExpressionListener";
  if (listener.script) listenerType = "scriptListener";
  return {
    ...JSON.parse(JSON.stringify(listener)),
    ...(listener.script ?? {}),
    listenerType: listenerType
  };
}

export const listenerTypeObject = {
  classListener: "Java 类",
  expressionListener: "表达式",
  delegateExpressionListener: "代理表达式",
  scriptListener: "脚本"
};

export const taskEventType = {
  create: "创建",
  assignment: "指派",
  complete: "完成",
  delete: "删除",
  update: "更新",
  timeout: "超时"
};
export const eventType = {
  start: "start",
  end: "end",
};

export const fieldTypeObject = {
  string: "字符串",
  expression: "表达式"
};

export const columns = (openListenerForm, removeListener, componentType) => {
  let _columns = [
    {
      title: "序号",
      width: "50px",
      key: "index",
      render: (_, __, index) => index + 1,
    },
    {
      title: "事件类型",
      dataIndex: "event",
      key: "event",
      ellipsis: true,
      render: (_, record) => {
        const eventTypes = componentType === "task" ? taskEventType : eventType;
        return eventTypes?.[record?.event]
      }
    },
    {
      title: "监听器类型",
      key: "listenerType",
      ellipsis: true,
      render: (_, record) => listenerTypeObject?.[record?.listenerType]
    },
    {
      title: "操作",
      width: 80,
      key: "operation",
      render: (_, record, idx) => {
        return <Space >
          <Button className={styles.btn} onClick={() => openListenerForm(record, idx)} type="link">编辑</Button>
          <Gconfirm
            content={"确认移除该字段吗？"}
            onConfirm={() => removeListener(idx)}>
            <Button className={styles.btn} type="link" danger>移除</Button>
          </Gconfirm>
        </Space >
      }
    },
  ]
  if (componentType === "task") {
    _columns.splice(2, 0, {
      title: "监听器ID",
      dataIndex: "id",
      key: "id",
      ellipsis: true,
    })
  }
  return _columns
}

export const listenerFieldFormColumns = (openListenerFieldForm, removeListenerField) => [
  {
    title: "序号",
    width: "50px",
    key: "index",
    render: (_, __, index) => index + 1,
  },
  {
    title: "字段名称",
    dataIndex: "name",
    key: "name",
    ellipsis: true,
  },
  {
    title: "字段类型",
    key: "fieldType",
    ellipsis: true,
    render: (_, record) => fieldTypeObject[record.fieldType]
  },
  {
    title: "字段值/表达式",
    key: "string",
    ellipsis: true,
    render: (_, record) => record.string || record.expression
  },
  {
    title: "操作",
    width: 80,
    key: "operation",
    render: (_, record, idx) => {
      return <Space >
        <Button onClick={() => openListenerFieldForm(record, idx)} type="link" style={{ padding: 0, fontSize: "12px", height: "18px" }}>编辑</Button>
        <Gconfirm
          content={"确认移除该字段吗？"}
          onConfirm={() => removeListenerField(idx)}>
          <Button type="link" danger style={{ padding: 0, fontSize: "12px", height: "18px" }}>移除</Button>
        </Gconfirm>
      </Space >
    }
  },
]